﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace Client
{
    public partial class FormWarnOrder : BaseChildForm
    {
        QunYing.BLL.ServiceOrder bll_so = new QunYing.BLL.ServiceOrder();
        public FormWarnOrder()
        {
            InitializeComponent();
            this.sharpGrid1.AllowPopupMenu = true;
            this.sharpGrid1.RowDoubleClickEvent += new EventHandler<Spore.Controls.RowDoubleClickEventArg>(sharpGrid1_RowDoubleClickEvent);
            BindData();
            GridView view = this.sharpGrid1.MainView as GridView;
           // view.BeforePrintRow += new DevExpress.XtraGrid.Views.Base.BeforePrintRowEventHandler(view_BeforePrintRow);
          
        }

      

        void sharpGrid1_RowDoubleClickEvent(object sender, Spore.Controls.RowDoubleClickEventArg e)
        {
            var row = this.GetSelectedRow();
            if (row == null)
                return;
            FormWarnOrderEdit fwd = new FormWarnOrderEdit();
            DataTable dt = row.Table.Clone();
            dt.ImportRow(row);
            var v = bll_so.DataTableToList(dt)[0];
            fwd.BindData(v);
            if (fwd.ShowDialog() == DialogResult.OK)
                this.BindData();

        }

        private DataRow GetSelectedRow()
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] rows = view.GetSelectedRows();
            if (rows.Length == 0)
                return null;
            foreach (var v in rows)
            {
                if (v < 0)
                    continue;
                DataRowView rowView = view.GetRow(v) as DataRowView;
                if (rowView == null)
                    return null;
                return rowView.Row;
            }
            return null;
        }

        public override void BindData()
        {
            if (GlobalInfo.UserLoginInfo.角色 == "经销商")
            {
                string whereStr = string.Format("经销商ID='{0}'", GlobalInfo.UserLoginInfo.用户ID);
                this.sharpGrid1.DataSource = bll_so.GetList(whereStr).Tables[0];
            }
            else
            {
                this.sharpGrid1.DataSource = bll_so.GetAllList().Tables[0];
            }
        }
    }
}